#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QString IP,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QString hostAddress = IP;
    //判断是否连接成功
    tcpsocket = new QTcpSocket; //通过new运算符创建新的QTcpsocket对象，并将其分配给tcpsocket指针

    quint16 port = 8888;
    tcpsocket->connectToHost(hostAddress, port); // 连接到服务器
    if (tcpsocket->waitForConnected(2000)) {
        log_message("连接成功");
    } else {
        log_message("连接失败");
    }

    // 设置初始背景图片
    this->setStyleSheet("#listView{background-image:url(:/image/room-0.png);}");

    // 设置 语音GIF 图片在 voice_gif_label 中显示
    voice_movie = new QMovie("../image/语音.gif");
    ui->voice_gif_label->setMovie(voice_movie);
    voice_movie->start();
    ui->voice_gif_label->hide();

    // 设置 语音GIF 图片在 music_gif_label 中显示
    music_movie = new QMovie("../image/音符跳动.gif");
    ui->music_gif_label->setMovie(music_movie);
    music_movie->start();
    ui->music_gif_label->hide();

    /*创建音频对象 */
    audio = new Audio(this);
    controller = new Controller(this);

    /*灯窗口*/
    wid_DeviceLamp = new DeviceLamp(LIVINGROOM,this);
    wid_DeviceLamp->move(300,120);
    wid_DeviceLamp->hide();
    /*空调窗口*/
    wid_DeviceAC = new DeviceAC(LIVINGROOM,this);
    wid_DeviceAC->move(1150,120);
    wid_DeviceAC->hide();
    /*音乐窗口*/
    wid_DeviceMusic = new DeviceMusic(LIVINGROOM,this);
    wid_DeviceMusic->move(600,120);
    wid_DeviceMusic->hide();
    /*电视窗口*/
//    wid_DeviceTV = new DeviceTV(this);
//    wid_DeviceTV->move(30,20);
//    wid_DeviceTV->hide();

    // 连接开关灯信号槽
    connect(wid_DeviceLamp, SIGNAL(update_background(bool)), this, SLOT(update_background(bool)));
    connect(wid_DeviceMusic, SIGNAL(update_foreground(bool)), this, SLOT(update_foreground(bool)));
    // 连接输出信息
    connect(wid_DeviceLamp, SIGNAL(log_message(QString)), this, SLOT(log_message(QString)));
    connect(wid_DeviceAC, SIGNAL(log_message(QString)), this, SLOT(log_message(QString)));
    connect(wid_DeviceMusic, SIGNAL(log_message(QString)), this, SLOT(log_message(QString)));
    // 连接语音控制（灯）
    connect(controller, SIGNAL(Light_ON()), wid_DeviceLamp, SLOT(simulateMousePressLight()));
    connect(controller, SIGNAL(Light_OFF()), wid_DeviceLamp, SLOT(simulateMousePressLight()));
    // 连接语音控制（空调）
    connect(controller, SIGNAL(AC_ON()), wid_DeviceAC, SLOT(simulateMousePressAC()));
    connect(controller, SIGNAL(AC_OFF()), wid_DeviceAC, SLOT(simulateMousePressAC()));
    // 连接语音控制（音乐）
    connect(controller, SIGNAL(Music_ON()), wid_DeviceMusic, SLOT(simulateMousePressMusic()));
    connect(controller, SIGNAL(Music_OFF()), wid_DeviceMusic, SLOT(simulateMousePressMusic()));

    /*启动定时器*/
    my_timer = new QTimer(this);
    // 初始化定时器和槽函数连接
    connect(my_timer,SIGNAL(timeout()),this,SLOT(slot_timeout()));
    my_timer->start(1000);

}

MainWindow::~MainWindow()
{
    delete ui;
    delete wid_DeviceLamp;
    delete wid_DeviceAC;
    delete wid_DeviceMusic;
//    delete wid_DeviceTV;
}
void MainWindow::slot_timeout()
{
    QTime currentTime = QTime::currentTime();
    QString timeText = currentTime.toString("hh:mm:ss"); // 添加秒钟显示
    ui->label_time_1->setText(timeText); // 更新第一个标签

    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString dateTimeText = currentDateTime.toString("yyyy-MM-dd ddd"); // 添加秒钟显示
    ui->label_time_2->setText(dateTimeText); // 更新第二个标签
}


void MainWindow::update_background(bool isOpen)
{
    if (isOpen)
    {
        // 将背景图片设置为开启状态的图片
        this->setStyleSheet("#listView{background-image:url(:/image/room-1.png);}");
    }
    else
    {
        // 将背景图片设置为关闭状态的图片
        this->setStyleSheet("#listView{background-image:url(:/image/room-0.png);}");
    }
}

void MainWindow::update_foreground(bool isOpen)
{
    if (isOpen)
    {
        ui->music_gif_label->show();
    }
    else {
        ui->music_gif_label->hide();
    }
}

// 添加输出信息
void MainWindow::log_message(QString message)
{
    tcpsocket->write(message.toUtf8());
    ui->log_cmd->append(message);
    my_speaker.say(message);
}

void MainWindow::on_DeviceAC_clicked()
{
    if (wid_DeviceAC->isHidden()) {
        wid_DeviceAC->show(); // 显示 Light 子页面
    } else {
        wid_DeviceAC->hide(); // 隐藏 Light 子页面
    }
}

void MainWindow::on_DeviceLamp_clicked()
{
    if (wid_DeviceLamp->isHidden()) {
        wid_DeviceLamp->show(); // 显示 Light 子页面
    } else {
        wid_DeviceLamp->hide(); // 隐藏 Light 子页面
    }
}


void MainWindow::on_DeviceMusic_clicked()
{
    if (wid_DeviceMusic->isHidden()) {
        wid_DeviceMusic->show(); // 显示 Light 子页面
    } else {
        wid_DeviceMusic->hide(); // 隐藏 Light 子页面
    }
}

void MainWindow::on_DeviceTV_clicked()
{
//    if (wid_DeviceTV->isHidden()) {
//        wid_DeviceTV->show(); // 显示 Light 子页面
//    } else {
//        wid_DeviceTV->hide(); // 隐藏 Light 子页面
//    }

}


void MainWindow::on_liyankang_pressed()
{
    ui->liyankang->setText("我是小李");

    // 加载并显示 GIF 图片
    ui->voice_gif_label->show();

    // 开始录音
    audio->startAudio("audiofile");

}


void MainWindow::on_liyankang_released()
{
    ui->liyankang->setText("");

    /* 停止录音 */
    audio->stopAudio();

    /* 启动识别 */
    QString retStr = audio->startSpeech();
//    my_speaker.say(retStr);//说话

    controller->instructDistribution(retStr);

    // 不显示 GIF图片
    ui->voice_gif_label->hide();
}






